const initalState = {
    toDoinge: JSON.parse(localStorage.getItem('toDoinge')) || [],
    toFinish: JSON.parse(localStorage.getItem('toFinish')) || [],
}
export function todoReducer(state = initalState,action) {

    switch (action.type) {
        case 'addTodo':
            var flag = false
            state.toDoinge.forEach(ele => {
                if (ele.text === action.text) {
                    window.alert('不能添加重复的todo')
                    flag = true
                }
            })
            if (!flag) {
                var todo = {
                    text: action.text,
                    status: 'todoinge'
                }
                state.toDoinge.push(todo)
                localStorage.setItem('toDoinge',JSON.stringify(state.toDoinge))
            }
            return {
                toDoinge: [...state.toDoinge],
                toFinish: [...state.toFinish],
            }
        case 'addFish':
            state.toDoinge.forEach((ele,index)=>{
                if(ele.text === action.todo.text){
                    state.toDoinge.splice(index,1)
                }
            })
            state.toFinish.push(action.todo)
            localStorage.setItem('toFinish',JSON.stringify(state.toFinish))
            action.status = 'tofinish'
            return{
                toDoinge: [...state.toDoinge],
                toFinish: [...state.toFinish]
            }
        default:
            return state
    }
}